<template>
  <div>
    <el-row>
      <el-col>
        <my-form
          :formOptions="findFormOptions"
          :isInline="true"
          :isSearch="true"
          @sureSubmit="findTableData"
        >
          <template #bottom_button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              type="primary"
              @click="reset"
              >重置</el-button
            >
          </template>
        </my-form>
      </el-col>
    </el-row>
    <el-row type="flex" justify="end">
      <el-col :span="2">
        <el-button size="mini" @click="add" icon="el-icon-circle-plus"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <my-table
          :hasBorder="true"
          :hasIndex="true"
          :hasPagination="true"
          :hasStripe="true"
          :hasSelection="false"
          :tableColumnOptions="tableColumnOptions"
          :tableData="tableData"
        >
          <template #operation="{ data }">
            <el-button type="text" @click="update(data)">编辑</el-button>
            <el-button type="text" @click="remove(data)">删除</el-button>
          </template>
        </my-table>
      </el-col>
    </el-row>
    <my-dialog :dialogVisible.sync="infoDialog" :title="infoTitle">
      <template #content>
        <my-form
          :form="isAddInfo ? copyForm : form"
          :formOptions="infoFormOptions"
           
          :rules="infoRules"
          @sureSubmit="infoFormSubmit"
        >
        </my-form>
      </template>
    </my-dialog>
  </div>
</template>

<script>
import myForm from "@/components/myForm.vue";
import myTable from "@/components/myTable.vue";
import myDialog from "@/components/myDialog.vue";
import {
  A_tableData,
  A_add,
  A_update,
  A_remove,
} from "@/api/resultGrade/instructStudentsGrade/academicPaperGrade";
import cloneDeep from "lodash/cloneDeep";
export default {
  name: "researchPaperGrade2",
  inject: ["reload"],
  data() {
    return {
      findFormOptions: [{ type: "input", label: "名称", model: "lnormalName" }],
      tableColumnOptions: [
        {
          label: "名称",
          prop: "lnormalName",
          width: "150",
        },
        {
          label: "业绩分值",
          prop: "lnormalScore",
          width: "150",
        },
        {
          label: "分值权重",
          prop: "lnormalWeight",
          width: "150",
        },
        {
          label: "选择类别",
          prop: "lnormalCategory",
          width: "150",
        },
        {
          label: "层次",
          prop: "lnormalLevel",
          width: "150",
        },
        {
          label: "操作",
          width: "180",
          isSlot: true,
          slotName: "operation",
          fixed: "right",
        },
      ],
      tableData: [],
      infoDialog: false,
      infoTitle: "编辑操作",
      isAddInfo: false,
      form: {},
      copyForm: {},
      infoFormOptions: [
        {
          type: "input",
          label: "名称",
          model: "lnormalName",
        },
        {
          type: "number",
          label: "业绩分值",
          model: "lnormalScore",
        },
        {
          type: "number",
          label: "分值权重",
          model: "lnormalWeight",
        },
        {
          type: "input",
          label: "类别",
          model: "lnormalCategory"
        },
        {
          type: "input",
          label: "层次",
          model: "lnormalLevel",
        },
      ],
      infoRules: {
        lnormalName: [{ required: true, message: "请输入名称" }],
        lnormalScore: [{ required: true, message: "请输入业绩分值" }],
        lnormalWeight: [{ required: true, message: "请输入分值权重" }],
        lnormalCategory: [{ required: true, message: "请输入选择类别" }],
        lnormalLevel: [{ required: true, message: "请输入层次" }],
      },
    };
  },
  components: {
    myForm,
    myTable,
    myDialog,
  },
  created() {
    this.getTableData();
  },
  methods: {
    async findTableData(data) {
      let result = await A_tableData(data);
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    async getTableData() {
      let result = await A_tableData({});
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    async reset() {
      let result = await A_tableData({});
      if (result.code === 1) {
        this.tableData = cloneDeep(result.data);
      } else {
        this.$message({ type: "error", message: "信息获取失败" });
      }
    },
    add() {
      this.isAddInfo = true;
      this.infoDialog = true;
    },
    update(data) {
      this.isAddInfo = false;
      this.infoDialog = true;
      this.form = cloneDeep(data);
    },
    remove(data) {
      this.$confirm("你确定要删除该信息吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { lnormalKey } = data;
          let res = await A_remove(lnormalKey);
          if (res.code === 1) {
            this.$message({ type: "success", message: res.data });
            this.reload();
          } else {
            this.$message({ type: "error", message: res.msg });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async infoFormSubmit(data) {
      if (this.isAddInfo) {
        let res = await A_add(data);
        if (res.code === 1) {
          this.$message({ type: "success", message: res.data });
          this.reload();
        } else {
          this.$message({ type: "error", message: res.msg });
        }
      } else {
        let res = await A_update(data);
        if (res.code === 1) {
          this.$message({ type: "success", message: res.data });
          this.reload();
        } else {
          this.$message({ type: "error", message: res.msg });
        }
      }
    },
  },
  computed: {},
};
</script>

<style scoped>
.upload-demo {
  display: inline-block;
  margin: 0 3px;
}
</style>